using System.Collections;
using DG.Tweening;
using UnityEngine;

public class Sun : MonoBehaviour
{
    public float interval; //间隔
    public int sun;
    public Vector3 targetPos;//目标位置
    public float speed = 0.1f;//掉落速度
    private bool isDestroy;//是否已经执行销毁计时
    bool isMoving;

    private void Update()
    {
        if (targetPos != Vector3.zero && Vector3.Distance(targetPos, transform.position) > 0.1f)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPos, speed);
        }
        else
        {
            if (isDestroy == false) StartCoroutine(SetDestroy());
        }
    }

    IEnumerator SetDestroy()
    {
        isDestroy = true;
        yield return new WaitForSeconds(interval); // 等待interval秒
        Destroy(gameObject);
    }

    //当鼠标或手指经过物体时触发
    public void OnMouseOver()
    {
        if (isMoving) return;
        isMoving = true;
        transform.DOMove(Camera.main.ScreenToWorldPoint(UIManager.Instance.pickUpPos.position), 0.5f).OnComplete(
            () =>
            {
                GameManager.Instance.SetSunSum(sun);
                AudioManager.Instance.PlaySFX("拾取阳光");
                Destroy(gameObject);
            }
        );
    }

    //设置目标位置
    public void SetTargetPos(Vector3 pos)
    {
        targetPos = pos;
    }
}
